package com.example.find.activity;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import com.example.find.R;
import com.example.find.R2;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class GetCodeActivty extends AppCompatActivity{

    @BindView(R2.id.btn_get_code)
    Button btnGetCode;

    private TimeCount time;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_get_code_layout);
        ButterKnife.bind(this);
        time = new TimeCount(60000, 1000);//构造CountDownTimer对象
    }

    @OnClick({R2.id.btn_get_code})
    public void onClick(View view){
        int id = view.getId();
        if(id == R.id.btn_get_code){
            time.start();
        }
    }

    class TimeCount extends CountDownTimer {
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);//参数依次为总时长,和计时的时间间隔
        }

        @Override
        public void onFinish() {//计时完毕时触发
            btnGetCode.setText("获取验证码");
            btnGetCode.setClickable(true);
            btnGetCode.setBackground(getResources().getDrawable(R.drawable.shape_btn_bg1));
        }

        @Override
        public void onTick(long millisUntilFinished) {//计时过程显示
            btnGetCode.setClickable(false);
            btnGetCode.setBackground(getResources().getDrawable(R.drawable.shape_btn_bg2));
            btnGetCode.setText(millisUntilFinished / 1000 + "秒后可重新获取");
        }
    }
}
